package cn.liasoux.utils;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import cn.liasoux.common.Mail;
import cn.liasoux.common.MsgLog;
import cn.liasoux.mapper.MsgLogMapper;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class ProducerUtil {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private MsgLogMapper msgLogMapper;

    @Value("${spring.rabbitmq.mail.router}")
    private String MAIL_ROUTER_KEY;

    @Value("${spring.rabbitmq.mail.exchange}")
    private String MAIL_EXCHANGE_NAME;

    public void sendEmail(Mail mail){
        String uuid = IdUtil.fastUUID();
        mail.setMsgId(uuid);
        MsgLog msgLog = new MsgLog();
        msgLog.setMsg(JSONUtil.toJsonStr(mail));
        msgLog.setMsgId(uuid);
        msgLog.setExchange(MAIL_EXCHANGE_NAME);
        msgLog.setRoutingKey(MAIL_ROUTER_KEY);
        msgLogMapper.insert(msgLog);
        rabbitTemplate.setExchange(MAIL_EXCHANGE_NAME);
        rabbitTemplate.setRoutingKey(MAIL_ROUTER_KEY);
        rabbitTemplate.convertAndSend(JSONUtil.toJsonStr(mail));
    }


}
